using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;

namespace GameXNA_Master
{

    class SavegameHandler
    {

        public void saveSettings(SettingsData settingsdata, RenderingData rendering_data, OptionMenu screens_menu)
        {
           /* new Thread(() =>
           {
               

           }).Start();*/
            settingsdata.heightResolution = (int)rendering_data.screenHeightResolutions[screens_menu.selRes];
            settingsdata.widhtResolution = (int)rendering_data.screenWidthtResolutions[screens_menu.selRes];
            
            settingsdata.isFullScreen = screens_menu.FullScreen;
            settingsdata.selectedResolution = screens_menu.selRes;
            settingsdata.useJoystic = screens_menu.useJoystic;

            rendering_data.screenHeight = settingsdata.heightResolution;
            rendering_data.screenWidth = settingsdata.widhtResolution;

            using (var stream = File.Open("savesetting.sd", FileMode.Create))
            {
                var binary_formatter = new BinaryFormatter();
                binary_formatter.Serialize(stream, settingsdata);
                Console.WriteLine("SALVATAGGIO");
            }
        }

        public SettingsData loadSettings()
        {
            SettingsData data;
            if (File.Exists("savesetting.sd"))
            {
                using (var stream = File.Open("savesetting.sd", FileMode.Open))
                {
                    var binary_formatter = new BinaryFormatter();
                    data = (SettingsData)binary_formatter.Deserialize(stream);

                }
            }
            else
            {
                data = new SettingsData();
                data.heightResolution = 600;
                data.widhtResolution = 800;
                data.isFullScreen = false;
            }
            return data;
        }


        public void saveGame(BulletLogic.GameState gamestate)
        {
            using (var stream = File.Open("savegame.sav", FileMode.Create))
            {
                var binary_formatter = new BinaryFormatter();
                binary_formatter.Serialize(stream, gamestate);
                gameInfoState.some_To_Loading = true;
                Console.WriteLine("SAVE ESEGUITO");

            }
        }


        public BulletLogic.GameState loadGame(out bool loaded)
        {
            BulletLogic.GameState data;

            if (File.Exists("savegame.sav"))
            {
                using (var stream = File.Open("savegame.sav", FileMode.Open))
                {
                    var binary_formatter = new BinaryFormatter();
                    try
                    {
                        data = (BulletLogic.GameState)binary_formatter.Deserialize(stream); 
                        loaded = true;
                    }
                    catch(SerializationException e)
                    {
                        Console.WriteLine("Attenzione: "+e.Message);
                        data = BulletLogic.projectiles_enemies_State;
                        loaded = false;
                    }
                    catch(ArgumentException ea)
                    {
                        Console.WriteLine("Attenzione: " + ea.Message);
                        data = BulletLogic.projectiles_enemies_State;
                        loaded = false;
                    }
                }
                
            }
            else
            {
                Console.WriteLine("NO CARICA");
                data = BulletLogic.projectiles_enemies_State;
                loaded = false;
            }

            return data;
        }

    }
}
